// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  const { page = 1, pageSize = 12 } = event
  
  try {
    console.log('获取背景列表，参数:', { page, pageSize })
    
    // 计算总数
    const countResult = await db.collection('backgrounds').count()
    const total = countResult.total
    
    // 分页查询
    const skip = (page - 1) * pageSize
    const backgrounds = await db.collection('backgrounds')
      .orderBy('createTime', 'desc')
      .skip(skip)
      .limit(pageSize)
      .get()
    
    console.log('查询结果:', backgrounds)
    
    return {
      success: true,
      data: backgrounds.data,
      total: total,
      page: page,
      pageSize: pageSize
    }
    
  } catch (error) {
    console.error('获取背景列表失败:', error)
    return {
      success: false,
      error: error.message
    }
  }
} 